<script setup lang='ts'>
  import { inject, nextTick, reactive, ref } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { ToastError, ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogTableVisible:false,
    dialogFormVisible:false,
    disabled:false,
    list:[]
  })
  const certifiedOptions = inject('certifiedOptions') as Array<any>
  interface formQuert {
    room_id:number | string
    certified:number
    desc:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    certified: [{ required: true, trigger: 'blur', message: '请选择认证类型' }],
    desc: [{ required: true, trigger: 'blur', message: '请填写认证描述' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      guild_roomApi.setCertified(form.room_id,form).then(async() => {
        ToastSuccess('修改成功')
        loading.close()
        await getList()
        closeDialog()
        $emit('updateList')
      }).catch(() => {
        loading.close()
      });
    })
  }
  const openForm = ()=>{
    action.dialogFormVisible = true
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await guild_roomApi.getRoomOpLog(form.room_id)
    const { data=[] } = res
    action.list = data
    loading.close()
  }
  const openDialog = async(query:any = {})=> {
    const { room_id=0 } = query
    if(!room_id) return ToastError('room_id error')
    form.room_id = room_id
    await getList()
    action.dialogTableVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog xm-dialog-cont-y"
    v-model="action.dialogTableVisible"
    width="60%"
    title="操作日志">
      <el-button class="xm-mb10 xm-mrAuto" type="primary" @click="openForm">认 证</el-button>
      <el-table class="xm-TableCenter" :data="action.list" border>
        <el-table-column width="200" label="操作时间" prop="created_at" />
        <el-table-column width="200" label="操作人" prop="users.name" />
        <el-table-column min-width="200" label="认证描述" prop="desc" />
        <el-table-column width="200" label="操作描述" prop="op" />
      </el-table>
    </el-dialog>
    <el-dialog
    top="15vh"
    class="input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="50%"
    title="认证房间">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="认证类型" prop="certified">
          <el-select v-model="form.certified" placeholder="请选择认证类型">
            <el-option
            v-for="item in certifiedOptions"
            :label="item.label"
            :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="认证描述" prop="desc">
          <el-input
            :disabled="action.disabled"
            v-model="form.desc"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请填写认证描述"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>